package com.slz3.business.module.orderManagement.ordOrderLogistics.steward;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.response.RR;
import com.slz3.business.frame.response.ResInfo;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.task.DynamicScheduleTaskSecond;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.orderManagement.ordOrder.domain.OrdOrder;
import com.slz3.business.module.orderManagement.ordOrderLogistics.domain.OrdOrderLogistics;
import com.slz3.business.module.orderManagement.ordOrderLogistics.domain.OrdOrderLogisticsFactory;
import org.springframework.data.domain.Page;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class OrdOrderLogisticsResExecutor {

    protected Mono<ServerResponse> add(ServerRequest req, OrdOrderLogistics ordOrderLogistics) {
        OrdOrderLogistics ordOrderLogisticsDBLatest = Services.ordOrderLogistics.save(ordOrderLogistics);
        try {
            Map map = Jedis.db0.getMap(RK.SERVICE__ORDER__INFO__ORDER_UUID__$UUID$.replace("${uuid}", ordOrderLogisticsDBLatest.getOrderUuid().toString()));
            map.put("LOGISTICSNUM",ordOrderLogisticsDBLatest.getLogisticsNo());
            map.put("LOGISTICSCOMPANY",ordOrderLogisticsDBLatest.getLogisticsCompany());
            map.put("MESSAGETYPE","SMS_151996608");
            Object memberUUid = map.get("serMemberUuid");
            //根据订单查询用户信息
            List list = new ArrayList<UUID>();
            list.add(UUID.fromString(memberUUid.toString()));
            List allByUuids1 = Services.memMember.findAllByUuids(list);
            DynamicScheduleTaskSecond.sendMessage("觅上物流信息",allByUuids1,map);
            /*SmsUtil.sendSms(map.get("receiverPhone").toString(),content);*/
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(ordOrderLogisticsDBLatest).build());
    }

    /**
     * known: ordOrderLogisticsList is not null
     */
    protected Mono<ServerResponse> delete(ServerRequest req) {
        List<OrdOrderLogistics> ordOrderLogisticsList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(ordOrderLogisticsList)) {
            List<OrdOrderLogistics> ordOrderLogisticsListNew = Services.ordOrderLogistics.delete(ordOrderLogisticsList);
            if (VE.isNotEmpty(ordOrderLogisticsListNew) && ordOrderLogisticsListNew.size() == ordOrderLogisticsList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(ordOrderLogisticsListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("delete fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    protected Mono<ServerResponse> modify(ServerRequest req, OrdOrderLogistics source) {
        OrdOrderLogistics target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            Steward.merge(source, target);
            OrdOrderLogistics ordOrderLogisticsDBLatest = Services.ordOrderLogistics.modify(target);
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(ordOrderLogisticsDBLatest).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    protected Mono<ServerResponse> list(ServerRequest req) {
        Page<OrdOrderLogistics> ordOrderLogisticsPage = Services.ordOrderLogistics.page(req.queryParams());
        ordOrderLogisticsPage.getContent().forEach(ordOrderLogistics ->
            ordOrderLogistics.setOrdOrder(Services.ordOrder.single(ordOrderLogistics.getOrderUuid()).orElse(new OrdOrder()))
        );
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(ordOrderLogisticsPage).build());
    }

    protected Mono<ServerResponse> info(ServerRequest req) {
        OrdOrderLogistics target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(target).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    protected Mono<ServerResponse> query(ServerRequest req) {
        OrdOrderLogistics target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(target).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    protected Mono<ServerResponse> choice(ServerRequest req) {
        Page<OrdOrderLogistics> ordOrderLogisticsPage = Services.ordOrderLogistics.page(req.queryParams());
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(ordOrderLogisticsPage).build());
    }

    /**
     * known: ordOrderLogisticsList is not null
     */
    protected Mono<ServerResponse> auditSubmit(ServerRequest req) {
        List<OrdOrderLogistics> ordOrderLogisticsList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(ordOrderLogisticsList)) {
            ordOrderLogisticsList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.WAIT_AUDIT);
                OrdOrderLogisticsFactory.initBaseFields(item);
            });
            List<OrdOrderLogistics> ordOrderLogisticsListNew = Services.ordOrderLogisticsTransactional.batchModify(ordOrderLogisticsList);
            if (VE.isNotEmpty(ordOrderLogisticsListNew) && ordOrderLogisticsListNew.size() == ordOrderLogisticsList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(ordOrderLogisticsListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditSubmit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: ordOrderLogisticsList is not null
     */
    protected Mono<ServerResponse> audit(ServerRequest req) {
        List<OrdOrderLogistics> ordOrderLogisticsList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(ordOrderLogisticsList)) {
            ordOrderLogisticsList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.APPROVED);
                OrdOrderLogisticsFactory.initBaseFields(item);
            });
            List<OrdOrderLogistics> ordOrderLogisticsListNew = Services.ordOrderLogisticsTransactional.batchModify(ordOrderLogisticsList);
            if (VE.isNotEmpty(ordOrderLogisticsListNew) && ordOrderLogisticsListNew.size() == ordOrderLogisticsList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(ordOrderLogisticsListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("audit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: ordOrderLogisticsList is not null
     */
    protected Mono<ServerResponse> auditReject(ServerRequest req) {
        List<OrdOrderLogistics> ordOrderLogisticsList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(ordOrderLogisticsList)) {
            ordOrderLogisticsList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.REJECT);
                OrdOrderLogisticsFactory.initBaseFields(item);
            });
            List<OrdOrderLogistics> ordOrderLogisticsListNew = Services.ordOrderLogisticsTransactional.batchModify(ordOrderLogisticsList);
            if (VE.isNotEmpty(ordOrderLogisticsListNew) && ordOrderLogisticsListNew.size() == ordOrderLogisticsList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(ordOrderLogisticsListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: ordOrderLogisticsList is not null
     */
    protected Mono<ServerResponse> usingEnable(ServerRequest req) {
        List<OrdOrderLogistics> ordOrderLogisticsList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(ordOrderLogisticsList)) {
            ordOrderLogisticsList.stream().forEach(item -> {
                item.setSerUsing(UsingFlag.ENABLE);
                OrdOrderLogisticsFactory.initBaseFields(item);
            });
            List<OrdOrderLogistics> ordOrderLogisticsListNew = Services.ordOrderLogisticsTransactional.batchModify(ordOrderLogisticsList);
            if (VE.isNotEmpty(ordOrderLogisticsListNew) && ordOrderLogisticsListNew.size() == ordOrderLogisticsList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(ordOrderLogisticsListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }


    /**
     * known: ordOrderLogisticsList is not null
     */
    protected Mono<ServerResponse> usingDisable(ServerRequest req) {
        List<OrdOrderLogistics> ordOrderLogisticsList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(ordOrderLogisticsList)) {
            ordOrderLogisticsList.stream().forEach(item -> {
                item.setSerUsing(UsingFlag.DISABLE);
                OrdOrderLogisticsFactory.initBaseFields(item);
            });
            List<OrdOrderLogistics> ordOrderLogisticsListNew = Services.ordOrderLogisticsTransactional.batchModify(ordOrderLogisticsList);
            if (VE.isNotEmpty(ordOrderLogisticsListNew) && ordOrderLogisticsListNew.size() == ordOrderLogisticsList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(ordOrderLogisticsListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("usingDisable fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

}
